package com.weiyuwqj.controller.user;

import com.github.pagehelper.Page;
//import com.weiyuwqj.api.model.vo.CalendarVo;
import com.weiyuwqj.api.service.BusInvestCollectService;
import com.weiyuwqj.api.service.BusInvestService;
import com.weiyuwqj.api.common.PageList;
import com.weiyuwqj.api.common.Paginator;
//import com.weiyuwqj.api.service.SysUserSignInService;
import com.weiyuwqj.api.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhangyong on 2016/8/22.
 * 日历
 */
@Controller
public class UserCalendarController {

    @Autowired
    BusInvestService investService;

    @Autowired
    BusInvestCollectService investCollectService;

/*    @Autowired
    SysUserSignInService signInService;*/

    /**
     * 日历显示
     * @return
     */
    @RequestMapping("/user/calendar")
    public Object calendar(){
        return "main/user/calendar";
    }

    /**
     * 日历显示
     * @return
     */
    @ResponseBody
    @RequestMapping("/user/calendarDisplayList")
    public Object calendarDisplayList(HttpServletRequest request){
        /*Integer userId=(Integer)request.getSession().getAttribute("userId");
        String dateStr=request.getParameter("date");
        //时间格式转换
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM");
        Date date=null;
        try {
            date=sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //获取当月起始时间和结束时间
        Date beginDate =DateUtils.getMonthStartTime(date);
        Date endDate =DateUtils.getNextMonthStartTime(date);
        Map map =new HashMap();
        Map param=new HashMap();
        map.put("userId",userId);
        map.put("beginDate",beginDate);
        map.put("endDate",endDate);
        List<CalendarVo> investList = investService.getCalendarList(map);
        List<CalendarVo> repayList = investCollectService.getCalendarList(map);
        List<CalendarVo> signList = signInService.getCalendarList(map);
        investList.addAll(repayList);
        investList.addAll(signList);
        Map result = new HashMap();
        Map data = new HashMap();
        for (CalendarVo calendarVo : investList ){
            String dealDate = calendarVo.getDealDate();
            String dealType = calendarVo.getDealType();
            if(data.get(dealDate) == null){
                data.put(dealDate,calendarVo.getDealType());
            }else {
                String type = String.valueOf(data.get(dealDate));
                data.put(dealDate,dealType+","+type);
            }
        }
        result.put("data",data);
        result.put("monthMaxDay",DateUtils.getMonthLastDay(date));
        result.put("firstDayWeek",DateUtils.getWeekOfDate(date));
        result.put("nowSignDays",signInService.getNowSignDays(new Date(),userId));
        return result;*/
        return null;
    }

    /**
     * 列表
     */
    @ResponseBody
    @RequestMapping("/user/calendarList")
    public Object calendarList(Paginator p,HttpServletRequest request,String viewDate){
return null;
       /* Integer start = (p.getPageNum()-1)*p.getPageSize();
        Integer end = p.getPageNum()*p.getPageSize()-1;
        if(p.getPageSize()==0){
            p.setPageSize(10);
        }
        if(p.getPageNum()==0){
            p.setPageNum(1);
        }

        Integer userId=(Integer)request.getSession().getAttribute("userId");
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date date=null;
        try {
            date=sdf.parse(viewDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Date startDate=DateUtils.getDayStartTime(date);
        Date endDate=DateUtils.getNextDayStartTime(date);
        Map param=new HashMap();
        List list=investService.getListInvestAndRepay(startDate,endDate,userId);

        Long count=(long)list.size();

        Page result = new Page();
        result.setTotal(count);
        for (int i=start;i<=end && i<list.size();i++){
            result.add(list.get(i));
        }
        p.init(count,list.size(),null);
        PageList pageList = new PageList(result);
        pageList.setPaginator(p);

        param.put("investRepayList", pageList);
        return pageList;*/
    }

    /**
     * 签到
     */
    @ResponseBody
    @RequestMapping("/user/signIn")
    public Object signIn(Integer userId){
        return null;//signInService.signIn(userId);
    }
}
